package com.kh.pajx.sop.view.action;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ApplicationAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.kh.pajx.sop.base.ModelDrivenBaseAction;
import com.kh.pajx.sop.domain.HsExpressRecord;
import com.kh.pajx.sop.domain.HsNotice;
import com.kh.pajx.sop.domain.SysDepartment;
import com.kh.pajx.sop.util.common.DwzAjaxResult;
import com.kh.pajx.sop.util.common.FormatDate;
import com.kh.pajx.sop.util.common.ValidatorUtils;
import com.opensymphony.xwork2.ActionContext;

/**
 * 快递信息 Action
 * @author mxl
 * @createDate 2015年4月7日 下午1:36:29
 */
@Controller
@Scope("prototype")
public class HsExpressAction extends ModelDrivenBaseAction<HsExpressRecord> implements ApplicationAware{
	private Map application;
	@Override
	public void setApplication(Map application) {
		this.application = application;
	}
	/**快递信息列表*/
	public String list(){
		try {
			ActionContext.getContext().put("expressCode",model.getExpressCode());
			hsExpressService.findList(model, getPageNum(), getNumPerPage());
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", "检索失败！"+e.getMessage());
		}
		return "list";
	}
	/**检查快递编号是否存在*/
	public void selectCode(){
		String code = StringUtils.trim(model.getExpressCode());
		try {
			List<HsExpressRecord> list = hsExpressService.getByCode(code);
			HttpServletResponse resp = ServletActionContext.getResponse();
			 PrintWriter out = resp.getWriter();
			    if (list.size() == 0) {
				out.print("true");
			    } else {
				out.print("false");
			    }
			    out.flush();
			    out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**添加快递登记信息页面*/
	public String addUi(){
		// 准备部门信息
		List<SysDepartment> departmentList = sysDepartmentService.getAll();
		ActionContext.getContext().put("departmentList", departmentList);
		return "addUi";
	}
	/**添加快递登记信息*/
	public void add(){
		DwzAjaxResult dwz = new DwzAjaxResult();
		try {
			if(!ValidatorUtils.validateLetterAndNum(model.getExpressCode(),20, false)){
				throw new Exception("属性[快递编号]输入错误，非法提交！");
			}
			if(!ValidatorUtils.validateLetterAndNum(model.getExpressName(),20, false)){
				throw new Exception("属性[快递公司名称]输入错误，非法提交！");
			}
			if(!ValidatorUtils.validateUUID(model.getReceiveOperId(),false)){
	    		throw new Exception("属性[接收人员]输入错误，非法提交！");
	    	}
			if(!ValidatorUtils.validateSafeString(model.getReceiveName(),20, false)){
				throw new Exception("属性[收件人]输入错误，非法提交！");
			}
			if(!ValidatorUtils.validateFixedLengthNumber(model.getReceivePhone(),11, false)){
				throw new Exception("属性[收件人联系方式]输入错误，非法提交！");
			}
			if(!ValidatorUtils.validateRelativeSafeString(model.getRemark(),50, true)){
				throw new Exception("属性[备注]输入错误，非法提交！");
			}
			model.setExpressCode(StringUtils.trim(model.getExpressCode()));
			model.setExpressName(model.getExpressName());
			model.setReceiveOperId(model.getReceiveOperId());
			model.setErStatus("1");
			model.setCreateOperId(getCurrentUser().getUserId());
			model.setCreateTime(FormatDate.dateTimeDetail());
			model.setReceiveName(StringUtils.trim(model.getReceiveName()));
			model.setReceivePhone(StringUtils.trim(model.getReceivePhone()));
			model.setRemark(StringUtils.trim(model.getRemark()));
			hsExpressService.save(model);
			
			//获取快递公司名称
			Map express = (Map) application.get("expressMap");
			String expressName = String.valueOf( express.get(model.getExpressName()));
			//System.out.println("快递公司："+expressName);
			//同时添加一条信息到公告表
			HsNotice notice = new HsNotice();
			notice.setTitle("快递接收提醒");
			notice.setContent("【"+expressName+"】  快递单号:"+model.getExpressCode()+"  "+model.getRemark());
			notice.setCreateTime(model.getCreateTime());
			notice.setCreateOperUser(model.getCreateOperId());
			notice.setIsSend("0");
			notice.setType("3");
			notice.setSubclass("1");
			notice.setReceiveUser(model.getReceiveOperId());//接收人编号
			hsNoticeService.save(notice);
			dwz.setMessage("保存成功！");
			dwz.setCallbackType("closeCurrent");
			dwz.setNavTabId("express_list");
			dwz.setStatusCode("200");
			DwzAjaxResult.josnMessage(dwz);
			
		} catch (Exception e) {

			e.printStackTrace();
			dwz.setMessage("保存失败！<br>错误信息：" + e.getMessage());
			dwz.setStatusCode("300");
			DwzAjaxResult.josnMessage(dwz);
		}
		
	}
	/**修改快递登记信息页面*/
	public String editUi(){
		// 准备部门信息
		List<SysDepartment> departmentList = sysDepartmentService.getAll();
		ActionContext.getContext().put("departmentList", departmentList);
		
		HsExpressRecord express=hsExpressService.getById(StringUtils.trim(model.getErid()));
		ActionContext.getContext().getValueStack().push(express);
		return "editUi";
	}
	/**修改快递登记信息*/
	public void edit(){
		DwzAjaxResult dwz = new DwzAjaxResult();
		try {
			if(!ValidatorUtils.validateUUID(model.getErid(),false)){
				throw new Exception("属性[主键标识]输入错误，非法提交！");
			}
			if(!ValidatorUtils.validateLetterAndNum(model.getExpressCode(),20, false)){
				throw new Exception("属性[快递编号]输入错误，非法提交！");
			}
			if(!ValidatorUtils.validateLetterAndNum(model.getExpressName(),20, false)){
				throw new Exception("属性[快递公司名称]输入错误，非法提交！");
			}
			if(!ValidatorUtils.validateUUID(model.getReceiveOperId(),false)){
				throw new Exception("属性[接收人员]输入错误，非法提交！");
			}
			if(!ValidatorUtils.validateSafeString(model.getReceiveName(),20, false)){
				throw new Exception("属性[收件人]输入错误，非法提交！");
			}
			if(!ValidatorUtils.validateFixedLengthNumber(model.getReceivePhone(),11, false)){
				throw new Exception("属性[收件人联系方式]输入错误，非法提交！");
			}
			if(!ValidatorUtils.validateRelativeSafeString(model.getRemark(),50, true)){
				throw new Exception("属性[备注]输入错误，非法提交！");
			}
			HsExpressRecord express=hsExpressService.getById(StringUtils.trim(model.getErid()));
			express.setExpressName(model.getExpressName());
			express.setReceiveOperId(model.getReceiveOperId());
			express.setReceiveName(StringUtils.trim(model.getReceiveName()));
			express.setReceivePhone(StringUtils.trim(model.getReceivePhone()));
			express.setRemark(StringUtils.trim(model.getRemark()));
			
			hsExpressService.update(express);
			dwz.setMessage("修改成功！");
			dwz.setCallbackType("closeCurrent");
			dwz.setNavTabId("express_list");
			dwz.setStatusCode("200");
			DwzAjaxResult.josnMessage(dwz);
			
		} catch (Exception e) {
			e.printStackTrace();
			dwz.setMessage("修改失败！<br>错误信息：" + e.getMessage());
			dwz.setStatusCode("300");
			DwzAjaxResult.josnMessage(dwz);
		}
	}
	/**查看快递信息*/
	public String show(){
		DwzAjaxResult dwz = new DwzAjaxResult();
		try {
			if(!ValidatorUtils.validateUUID(model.getErid(),false)){
	    		throw new Exception("属性[快递信息主键]输入错误，非法提交！");
	    	}
			HsExpressRecord express=hsExpressService.getById(StringUtils.trim(model.getErid()));
			ActionContext.getContext().getValueStack().push(express);
			return "show";
		} catch (Exception e) {
			
			e.printStackTrace();
			dwz.setStatusCode("300");
		    dwz.setConfirmMsg("查询错误" + e.getMessage());
		    DwzAjaxResult.josnMessage(dwz);
		    return null;
		}
	}
	
}
